import { Ref, ref, watch } from "vue";
import { controlDevice } from "../shared";

/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2023-01-31 16:26:11
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2023-01-31 17:35:18
 * @FilePath: \v2.0.0\src\views\patroling\composables\useLightControl.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
interface Options {
  caritem: Ref<string>;
}
export default (options: Options) => {
  const { caritem } = options;
  const frontLight = ref(false);
  watch(frontLight, (newVal) => {
    toggleLight(newVal, lightModes.FRONT);
  });

  const backLight = ref(false);
  watch(backLight, (newVal) => {
    toggleLight(newVal, lightModes.BACK);
  });

  const lightModes = {
    FRONT: "01",
    BACK: "02",
  };
  /**
   * @description: 切换大灯
   * @return {*}
   */
  function toggleLight(newVal, mode) {
    const data = {
      code: caritem.value,
      param1: "07",
      param2: newVal ? "01" : "00",
      param3: mode,
      param4: "00",
    };
    controlDevice(data);
  }
  function toggleFrontLight() {
    frontLight.value = !frontLight.value;
  }

  function toggleBackLight() {
    backLight.value = !backLight.value;
  }

  return {
    frontLight,
    backLight,
    toggleFrontLight,
    toggleBackLight,
  };
};
